package com.kitty.game.fight.artifact.active;


import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.game.utils.StatusConst;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.kitty.game.fight.artifact.active.ArtifactSkillProcessor;

/**
 * 嘲讽
 * 回合初为目标施加嘲讽状态，使以己方为主目标的攻击转为攻击嘲讽者。
 * 若嘲讽者倒地，切换目标为其宠物
 */
class SneerSkillProcessor extends ArtifactSkillProcessor {

    @Override
    protected boolean checkUseCondition(Fight fight, FightObject attacker) {
        List<FightObject> targets = fight.getTargetSide(attacker.getId());
        Set<Integer> targetIds = new HashSet<>();
        for (FightObject target : targets) {
            targetIds.add(target.getId());
        }
        long mainTarget = attacker.getTargetId();
        if (targetIds.contains(mainTarget)) {
            return false;
        }
        return true;
    }

    @Override
    public void binging(Fight fight, FightObject attacker,int specialSkillLevel) {
        int targetId = attacker.getTargetId();
        FightObject target = fight.queryFightObject(targetId);
        if (target == null) {
            return;
        }
        //嘲讽持续时间
        target.setStatus(StatusConst.STATUS_CHAOFENG, getLastRound(specialSkillLevel));
        //技能等级
        target.setStatusScore(StatusConst.STATUS_CHAOFENG, specialSkillLevel);
    }

    /**
     * 持续回合数根据等级计算
     * @return
     */
    private int getLastRound(int specialSkillLevel) {
        int skillLv = specialSkillLevel;
        if (skillLv >= 1 && skillLv <= 6) {
            return 2;
        }
        if (skillLv > 6 && skillLv <= 12) {
            return 3;
        }
        if (skillLv >12  && skillLv <= 18) {
            return 4;
        }
        if (skillLv > 18) {
            return 5;
        }
        return 0;
    }

    @Override
    public FightObject delayAction(Fight fight, FightObject attacker, Object... params) {
        // 寻找对方嘲讽者
        List<FightObject> targets = fight.getTargetSide(attacker.getId());
        FightObject changeTarget = null;
        for (FightObject member : targets) {
            if (member.isPlayer()) {
                int lastRound = member.getStatusScore(StatusConst.STATUS_CHAOFENG);
                if (lastRound >= fight.getRound()) {
                    // 若嘲讽者倒地，切换目标为其宠物
                    if (member.isFallDown()) {
                        FightObject petFightObj = fight.queryFightObject(member.getFightPetId()) ;
                        if (petFightObj != null && !petFightObj.isFallDown()) {
                            changeTarget = petFightObj;
                            break;
                        }
                    } else {
                        changeTarget = member;
                        break;
                    }
                }
            }
        }
        if (changeTarget != null) {
            attacker.setTargetId(changeTarget.getId());
        }

        return changeTarget;
    }

}